package com.iwhalecloud.bss.kite.cucc.order.save.impl;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.order.util.TableUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.IInst;
import com.iwhalecloud.bss.kite.dataservice.enums.TableConfig;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.ztesoft.bss.base.extmapper.BaseMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 实例入库处理器
 */
@Component
public class InstSaveHandler extends BaseSaveHandler<IInst> {

    @Override
    public void insert(List<IInst> insts, String tableName) {
        super.insert(insts, tableName);

        if (CollectionUtils.isEmpty(insts)) {
            return;
        }

        TableConfig extTableConfig = TableUtils.getExtTableConfig(tableName);
        if (Objects.nonNull(extTableConfig)) {
            List<IInst> extInsts = new ArrayList<>();
            for (IInst inst : insts) {
                IInst extInst = KiteBeanUtils.transform(inst, extTableConfig.getEntityClass());
                extInsts.add(extInst);
            }

            BaseMapper<IInst> extMapper = TableUtils.getMapper(extTableConfig.name(), extTableConfig.getEntityClass());
            extMapper.insertBatch(extInsts);
        }
    }

    @Override
    public void update(List<IInst> insts, String tableName) {
        if (CollectionUtils.isEmpty(insts)) {
            return;
        }

        TableConfig extTableConfig = TableUtils.getExtTableConfig(tableName);
        BaseMapper<IInst> extMapper = Objects.nonNull(extTableConfig) ?
            TableUtils.getMapper(extTableConfig.name(), extTableConfig.getEntityClass()) :
            null;

        BaseMapper<IInst> mapper = TableUtils.getMapper(tableName, insts.get(0).getClass());
        for (IInst inst : insts) {
            String actionType = inst.getActionType();
            if (Objects.equals(ActionType.M.name(), actionType)) {
                // 如果actionType == M，入库时改回K
                inst.setActionType(ActionType.K.name());
            }
            mapper.updateByPrimaryKeySupportSliceKey(inst);

            if (Objects.nonNull(extMapper)) {
                IInst extInst = KiteBeanUtils.transform(inst, extTableConfig.getEntityClass());
                extMapper.updateByPrimaryKeySupportSliceKey(extInst);
            }

            // 入库后还原actionType
            inst.setActionType(actionType);
        }
    }

    @Override
    public void updateSelective(List<IInst> insts, String tableName) {
        if (CollectionUtils.isEmpty(insts)) {
            return;
        }

        TableConfig extTableConfig = TableUtils.getExtTableConfig(tableName);
        BaseMapper<IInst> extMapper = Objects.nonNull(extTableConfig) ?
            TableUtils.getMapper(extTableConfig.name(), extTableConfig.getEntityClass()) :
            null;

        BaseMapper<IInst> mapper = TableUtils.getMapper(tableName, insts.get(0).getClass());
        for (IInst inst : insts) {
            String actionType = inst.getActionType();
            if (Objects.equals(ActionType.M.name(), actionType)) {
                // 如果actionType == M，入库时改回K
                inst.setActionType(ActionType.K.name());
            }
            mapper.updateByPrimaryKeySupportSliceKeySelective(inst);

            if (Objects.nonNull(extTableConfig)) {
                IInst extInst = KiteBeanUtils.transform(inst, extTableConfig.getEntityClass());
                String id = KiteBeanUtils.getValueByName(extInst, extTableConfig.getIdField());
                if (StringUtils.isNoneBlank(id, extInst.getOwnerCustId())) {
                    extMapper.updateByPrimaryKeySupportSliceKeySelective(extInst);
                }
            }

            // 入库后还原actionType
            inst.setActionType(actionType);
        }
    }

    @Override
    public void delete(List<IInst> insts, String tableName) {
        if (CollectionUtils.isEmpty(insts)) {
            return;
        }

        TableConfig extTableConfig = TableUtils.getExtTableConfig(tableName);
        String extIdField = "";
        String extSliceKeyField = "";
        BaseMapper<IInst> extMapper = null;
        if (Objects.nonNull(extTableConfig)) {
            extIdField = extTableConfig.getIdField();
            extSliceKeyField = extTableConfig.getSliceKeyField();
            extMapper = TableUtils.getMapper(extTableConfig.name(), extTableConfig.getEntityClass());
        }

        BaseMapper<IInst> mapper = TableUtils.getMapper(tableName, insts.get(0).getClass());
        for (IInst inst : insts) {
            mapper.deleteByPrimaryKeySupportSliceKey(inst);

            if (Objects.nonNull(extMapper)) {
                extMapper.deleteByPrimaryKeyAndSliceKey(KiteBeanUtils.getValueByName(inst, extIdField),
                    KiteBeanUtils.getValueByName(inst, extSliceKeyField));
            }
        }
    }

    @Override
    public void transfer(List<IInst> insts, String tableName) {
        if (CollectionUtils.isEmpty(insts)) {
            return;
        }

        TableConfig extTableConfig = TableUtils.getExtTableConfig(tableName);
        BaseMapper<IInst> extMapper = Objects.nonNull(extTableConfig) ?
            TableUtils.getMapper(extTableConfig.name(), extTableConfig.getEntityClass()) :
            null;

        List<IInst> insertInsts = new ArrayList<>();
        List<IInst> insertExtInsts = new ArrayList<>();
        BaseMapper<IInst> mapper = TableUtils.getMapper(tableName, insts.get(0).getClass());
        for (IInst inst : insts) {
            String custId = inst.getOwnerCustId();
            String newCustId = inst.getNewOwnerCustId();
            if (StringUtils.isBlank(newCustId) || KiteStringUtils.equals(custId, newCustId)) {
                continue;
            }

            mapper.deleteByPrimaryKeySupportSliceKey(inst);
            inst.setOwnerCustId(newCustId);
            insertInsts.add(inst);

            if (Objects.nonNull(extMapper)) {
                String extInstId = KiteBeanUtils.getValueByName(inst, extTableConfig.getIdField());
                if (StringUtils.isBlank(extInstId)) {
                    continue;
                }

                IInst extInst = KiteBeanUtils.transform(inst, extTableConfig.getEntityClass());
                extInst.setOwnerCustId(custId);
                extMapper.deleteByPrimaryKeySupportSliceKey(extInst);

                extInst.setOwnerCustId(newCustId);
                insertExtInsts.add(extInst);
            }
        }

        mapper.insertBatch(insertInsts);
        if (Objects.nonNull(extMapper) && CollectionUtils.isNotEmpty(insertExtInsts)) {
            extMapper.insertBatch(insertExtInsts);
        }
    }
}
